package com.skyeyemonitoring.activitys;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.skyeyemonitoring.R;
import com.skyeyemonitoring.base.BaseActivity;
import com.skyeyemonitoring.custom.MyMarkerView;

import java.util.ArrayList;
import java.util.Random;

/**
 * 震动数据
 * Created by ${QQZ} on 2017/5/11.
 */

public class LocaShakeActity extends BaseActivity {


    private LineChart mScakeChart;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qqz_local_shake_aty);
        setBackBtn("震动数据");
        initView();
        iniData();
    }

    private void iniData() {
        mScakeChart.setDrawGridBackground(false);

        mScakeChart.animateY(3000);//设置动画，效果为Y类型
        // no description text
        mScakeChart.getDescription().setEnabled(false);

        // enable touch gestures
        mScakeChart.setTouchEnabled(true);//是否可以触摸-启动触摸可以查看点击位置的值

        // enable scaling and dragging
        mScakeChart.setDragEnabled(true);
        mScakeChart.setScaleEnabled(true);

        // if disabled, scaling can be done on x- and y-axis separately
        mScakeChart.setPinchZoom(true);

        // set an alternative background color/背景色
        mScakeChart.setBackgroundColor(Color.parseColor("#262626"));

        // create a custom MarkerView (extend MarkerView) and specify the layout
        // to use for it
        MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
        mv.setChartView(mScakeChart); // For bounds control
        mScakeChart.setMarker(mv); // Set the marker to the chart

        XAxis xl = mScakeChart.getXAxis();
        xl.setAvoidFirstLastClipping(true);
        xl.setAxisMinimum(0f);

        YAxis leftAxis = mScakeChart.getAxisLeft();
        leftAxis.setInverted(true);
        leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

        YAxis rightAxis = mScakeChart.getAxisRight();
        rightAxis.setEnabled(false);

        // add data
//        setData(25, 50);
        Random random=new Random();

        int i = random.nextInt(random.nextInt(555));
        setDatas(i, i);

        // // restrain the maximum scale-out factor
//         mScakeChart.setScaleMinima(3f, 3f);
        //
        // // center the view to a specific position inside the chart
//         mScakeChart.centerViewPort(10, 50);

        // get the legend (only possible after setting data)
        Legend l = mScakeChart.getLegend();

        // modify the legend ...
        l.setForm(Legend.LegendForm.LINE);

        // dont forget to refresh the drawing
        mScakeChart.invalidate();
    
    }


    private void initView() {
        mScakeChart = (LineChart) findViewById(R.id.scake_chart);
    }
    /**
     * 三根弦的折线图
     * @param count
     * @param range
     */
    private void setDatas(int count, float range) {

        ArrayList<Entry> yVals1 = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            float mult = range / 2f;
            float val = (float) (Math.random() * mult) + 50;
            yVals1.add(new Entry(i, val));
        }

        ArrayList<Entry> yVals2 = new ArrayList<>();

        for (int i = 0; i < count-1; i++) {
            float mult = range;
            float val = (float) (Math.random() * mult) + 450;
            yVals2.add(new Entry(i, val));
        }

        ArrayList<Entry> yVals3 = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            float mult = range;
            float val = (float) (Math.random() * mult) + 500;
            yVals3.add(new Entry(i, val));
        }

        LineDataSet set1, set2, set3;

        if (mScakeChart.getData() != null && mScakeChart.getData().getDataSetCount() > 0) {
            //第一条折线
            set1 = (LineDataSet) mScakeChart.getData().getDataSetByIndex(0);
            //第二条折线
            set2 = (LineDataSet) mScakeChart.getData().getDataSetByIndex(1);
            //第三条折线
            set3 = (LineDataSet) mScakeChart.getData().getDataSetByIndex(2);
            set1.setValues(yVals1);
            set2.setValues(yVals2);
            set3.setValues(yVals3);
            mScakeChart.getData().notifyDataChanged();
            mScakeChart.notifyDataSetChanged();
        } else {
            // create a dataset and give it a type
            set1 = new LineDataSet(yVals1, "Series 1");

            set1.setAxisDependency(YAxis.AxisDependency.LEFT);
            set1.setColor(ColorTemplate.getHoloBlue());//线条颜色
            set1.setCircleColor(Color.WHITE);
            set1.setLineWidth(0.5f);//线条的宽度
            set1.setCircleRadius(3f);
            set1.setFillAlpha(65);
            set1.setFillColor(ColorTemplate.getHoloBlue());
            set1.setHighLightColor(Color.rgb(244, 117, 117));
            set1.setDrawCircleHole(false);
            set1.setDrawCircles(false);//取消折线上面的 点
            set1.setDrawValues(false);//是否显示折线位置数值
            //set1.setFillFormatter(new MyFillFormatter(0f));
            //set1.setDrawHorizontalHighlightIndicator(false);
            //set1.setVisible(false);
            //set1.setCircleHoleColor(Color.WHITE);

            // create a dataset and give it a type
            set2 = new LineDataSet(yVals2, "Series 2");
            set2.setAxisDependency(YAxis.AxisDependency.RIGHT);
            set2.setColor(Color.RED);//线条颜色
            set2.setCircleColor(Color.WHITE);
            set2.setLineWidth(0.5f);//线条的宽度
            set2.setCircleRadius(3f);
            set2.setFillAlpha(65);
            set2.setFillColor(Color.RED);
            set2.setDrawCircleHole(false);
            set2.setDrawCircles(false);//取消折线上面的 点
            set2.setHighLightColor(Color.rgb(244, 117, 117));
            set2.setDrawValues(false);//是否显示折线位置数值
            //set2.setFillFormatter(new MyFillFormatter(900f));

            set3 = new LineDataSet(yVals3, "Series 3");
            set3.setAxisDependency(YAxis.AxisDependency.RIGHT);
            set3.setColor(Color.YELLOW);//线条颜色
//            set3.setCircleColor(Color.WHITE);// 圆形的颜色,
            set3.setLineWidth(0.5f);//线条的宽度
            set3.setCircleRadius(3f);
            set3.setFillAlpha(65);
            set3.setFillColor(ColorTemplate.colorWithAlpha(Color.YELLOW, 200));
            set3.setDrawCircleHole(false);
            set3.setDrawCircles(false);//取消折线上面的 点
            set3.setDrawValues(false);//是否显示折线位置数值
            set3.setHighLightColor(Color.rgb(244, 117, 117));

            // create a data object with the datasets
            LineData data = new LineData(set1, set2, set3);
            data.setValueTextColor(Color.WHITE);
            data.setValueTextSize(9f);

            // set data
            mScakeChart.setData(data);
        }
    }
}
